import 'package:flutter/services.dart';
import 'package:vibration/vibration.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'base_handler.dart';

class DeviceControlHandler extends BaseHandler {

  @override
  String name = "deviceControl";

  @override
  void handler(dynamic data, dynamic responseCallback) async {
    String type = data['type'];
    if(type == 'vibrate'){
      dynamic param = data['param'];
      int duration = 500;
      if(param != null && param.containsKey('duration')) {
        duration = param['duration'];
      }
      bool canVibrate = await Vibration.hasVibrator();
      if (canVibrate) {
        Vibration.vibrate(duration: duration);
      }
      //振动
    }else if(type == 'direction'){
      // 切换屏幕方向
      String param = data['param'];
      if(param == 'landscape') {
        // 横屏
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.landscapeLeft,
          DeviceOrientation.landscapeRight
        ]);
      }else if(param == 'portrait') {
        // 竖屏
        SystemChrome.setPreferredOrientations([
          // 强制竖屏
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown
        ]);
      }
    }else if(type == 'wakeLock'){
      // 屏幕常亮
      bool param = data['param'];
      if(param) {
        WakelockPlus.enable();
      }else{
        WakelockPlus.disable();
      }

    }

  }
}